package com.kzd.gtgame.main.money

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.ProblemDetailsAction
import com.kzd.gtgame.main.money.adapter.MoneyTaskPlayAdapter
import com.kzd.gtgame.main.toProblemDetailsActivity
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_task_play.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class MoneyTaskPlayActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mMoneyTaskPlayAdapter = MoneyTaskPlayAdapter()

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_task_play

    override fun stateBar(): StatusBar {
        return StatusBar(view = tlMoneyTaskPlayBar)
    }

    override fun initView() {

        tlMoneyTaskPlayBar.setOnClickRightTextView {
            toProblemDetailsActivity(ProblemDetailsAction.PLAY)
        }

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMoneyTaskPlayAdapter

        mMoneyTaskPlayAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mMoneyTaskPlayAdapter.getItem(position)
            if (view.id == R.id.tvMoneyTaskPlayGo) {
                if (itemData.status == 0 || itemData.status == 2) {
                    viewModel.moneyTaskPlayGet(itemData.id)
                }
            }
        }

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.moneyTaskPlay()
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.moneyTaskPlay(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.moneyTaskPlay()
    }

    override fun initViewMode() {
        viewModel.moneyTaskPlayLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response, mMoneyTaskPlayAdapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMoneyTaskPlayAdapter.addData(data)
            } else {
                mMoneyTaskPlayAdapter.setNewInstance(data)
            }
        })

        viewModel.moneyTaskPlayGetLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            viewModel.moneyTaskPlay()
        })
    }
}